//
//  ChooseLabelController.m
//  Qzone
//
//  Created by zhangxy on 2018/6/19.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ChooseLabelController.h"
#import "MyButton.h"
#import "ZTextField.h"

#import "NavTypeEntity.h"
#import "ZZChooseLabelCell.h"
#define cellIdentifier @"ZZChooseLabelCell"

@interface ChooseLabelController ()<UITextFieldDelegate>{
    NSIndexPath *checkPath;
}

@property(nonatomic,strong) NSMutableArray *listArray;
@property(nonatomic,strong) NSMutableArray *navArray;

@property(nonatomic,strong) UITableView *listTable;
@property(nonatomic,strong) ZTextField *textField;

@end

@implementation ChooseLabelController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [super viewDidLoad];
    if(_type == 2){
     
        self.navigationItem.title = @"选择话题";
    }else{
        
        self.navigationItem.title = @"选择标签";
    }
    [self setNavigationBar];
    
    self.navigationController.navigationBarHidden = NO;
    
    [self.view setBackgroundColor:UIColorFromRGB(BgSystemColor)];
    [self createTableView];
}



-(void)createTableView{
    _listArray = [[NSMutableArray alloc] init];
    _navArray  = [[NSMutableArray alloc] init];
    
    UIView *topView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 10, ScreenWidth - 40, 44)];
    topView.layer.cornerRadius = 10.0f;
    topView.layer.masksToBounds = YES;
    [topView setBackgroundColor:UIColorFromRGB(TextWhiteColor)];
    [self.view addSubview:topView];
    
    UIImageView *imgView = [self createImageView];
    [imgView setImage:[UIImage imageNamed:@"img_search_copy"]];
    [imgView setFrame:CGRectMake(20, 15, 13, 13)];
    [topView addSubview:imgView];
    
    _textField = [[ZTextField alloc] initWithFrame:CGRectMake(48, 0, topView.frame.size.width - 48*2, 44)];
    [_textField setPlaceholder:@"请输入标签"];_textField.clipsToBounds = YES;
    _textField.delegate = self;
    _textField.font = FontFiftent;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textField.returnKeyType = UIReturnKeySearch;
    
    [_textField addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
    [topView addSubview:_textField];
    
    
    UIButton *btn = [self createButton];
    [btn setImage:[UIImage imageNamed:@"btn_search_close"] forState:0];
    [btn setTitle:@"" forState:0];
    [btn setFrame:CGRectMake(topView.frame.size.width - 48, 9, 20, 20)];
    [topView addSubview:btn];
    
    
    
    _listTable=[self.view createTableView:self cell:cellIdentifier];
    [_listTable setFrame:CGRectMake(20, 64, ScreenWidth - 40, self.view.frame.size.height-64)];
    _listTable.layer.cornerRadius = 10.0f;
    _listTable.layer.masksToBounds = YES;
    [_listTable setBackgroundColor:UIColorFromRGB(TextWhiteColor)];
    
    if (iOS7) {
        _listTable.backgroundView = nil;
    }
    UIView *footer =[ [UIView alloc]init];
    footer.backgroundColor = [UIColor clearColor];
    [_listTable setTableFooterView:footer];
    
    
    [_listTable setSeparatorColor:LINE_COLOR];
    [_listTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    
    
    [self setTableSeparatorInset];
    
    
    
    [self loadBegin];
}


-(void)loadBegin{
    [_listArray removeAllObjects];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self loadMoreData];
}


/**
 加载更多
 */
-(void)loadMoreData{
    NSString *cat_id = @"";
    if(_entity!=nil){
        cat_id = convertToString(_entity.cat_id);
    }
    [NetworkUtil POST:API_GetTagList parameters:@{@"cat_id":cat_id,@"size":@"30"} success:^(id responseObject, NSDictionary *dict) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSArray *arr = dict[@"data"];
        for (NSDictionary *item in arr) {
            NavTypeEntity *model = [[NavTypeEntity alloc] initWithDictionary:item];
            [self.listArray addObject:model];
        }
        
        [self.listTable reloadData];
    } failure:^(NSError *error, NSString *message) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}



#pragma mark UITableView delegate Start
// 返回section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

// 返回section高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  0;
}

// 返回section 的View
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
    if(section == 0){
        
    }
    return headerView;
}

// 返回section下得行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _listArray.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZZChooseLabelCell *cell = (ZZChooseLabelCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[ZZChooseLabelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NavTypeEntity *entity = [_listArray objectAtIndex:indexPath.row];
    if(checkPath!=nil && checkPath.row == indexPath.row){
        [cell dataToView:entity index:YES];
    }else{
        [cell dataToView:entity index:NO];
    }
    
    return cell;
}



// 是否显示删除功能
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

// 删除清理数据
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingStyle = UITableViewCellEditingStyleDelete;
}


// table 行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    return 70.0f;
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

// table 行的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NavTypeEntity *entity=[_listArray objectAtIndex:indexPath.row];
    if(_ZZChooseResultBlock){
        _ZZChooseResultBlock(entity);
    }
    [self backAction];
}

//设置分割线间距
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row < _listArray.count){
        UIEdgeInsets inset = UIEdgeInsetsMake(0, 10, 0, 0);
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:inset];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:inset];
        }
    }else{
        UIEdgeInsets inset = UIEdgeInsetsMake(0, ScreenWidth, 0, 0);
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:inset];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:inset];
        }
    }
}

-(void)viewDidLayoutSubviews{
    [self setTableSeparatorInset];
}
#pragma mark UITableView delegate end

/**
 *  设置UITableView分割线空隙
 */
-(void)setTableSeparatorInset{
    UIEdgeInsets inset = UIEdgeInsetsMake(0, 10, 0, 0);
    if ([_listTable respondsToSelector:@selector(setSeparatorInset:)]) {
        [_listTable setSeparatorInset:inset];
    }
    
    if ([_listTable respondsToSelector:@selector(setLayoutMargins:)]) {
        [_listTable setLayoutMargins:inset];
    }
}



#pragma mark - 搜索
/*
 每次输入文字都会进行搜索，但只有点击确认之后，才会写入搜索历史
 */
- (void)textFieldValueChanged:(UITextField *)textField
{
    DLog(@"text:%@",textField.text);
    if (textField.text.length > 0) {
       
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self searchAction];
    [self textFieldValueChanged:textField];
    return YES;
}

- (void)searchAction
{
    [_textField resignFirstResponder];
    
    if (_textField.text.length == 0) {
        return;
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
